;
var _pageUrl = "/htmlWeb/adv/list";
var curPage = 1, total = 0, pageSize = 10, totalPage = 0; //当前页码,总记录数，每页显示数，总页数
function getParam() {
    return {curPage: curPage <= 1 ? 1 : curPage,
        pageSize: pageSize,
        advTpye:2,
        parentId: $("#parentId").val()};
}
function getGrid(param) {
    $.ajax({
        url: _pageUrl,
        type: 'POST',
        data: param,
        dataType: "json",
        success: function (data) {
            total = data.total;
            pageSize = data.pageSize;
            totalPage = data.totalPage;
            var _gridHtml = '';
            $.each(data.rows, function (index, row) {
                _gridHtml += '<tr>' +
                    '<td><input type="hidden" class="rowid" value=\"' + row.id + '\"><p>' + row.id + '</p></td>' +
                    '<td><p>' + row.parentId + '</p></td>' +
                    '<td><p>' + row.name + '</p></td>' +
                    '<td><p><img src="http://img.boyitongcheng.com/'+row.imgUrl+'" width="60px" height="40px"/></p></td>' +
                    '<td><p>' + row.url + '</p></td>' +
                    '<td><p>' + row.provinceName + '</p></td>' +
                    '<td><p>' + row.cityName + '</p></td>' +
                    '<td><span class="userSpan"><p>' +
                    '<a href="javascript:edit(' + row.id +',' + row.parentId + ')">编辑</a>' +
                    // '<a href="javascript:detail(' + row.id + ')">详情</a>' +
                    '</p></span></td></tr>';
            });
            $("#advGrid1 tr:not(:first)").html("");
            $('#advGrid1').append(_gridHtml);
        },
        complete: function () {
            getPager();
        },
        error: function () {
            alert("数据加载失败");
        }
    });
}
function addAdv() {
    $.ajaxSetup({cache: false});
    $(".mainDiv").load('htmlWeb/systemAddAdverHtml', '', function () {
        $("#id").val(index);
        $("#opType").val(0);
        $("#advType").val(0);
        $.getScript("../../by-webhtml/assets/js/system/system_addAdver.js");
    });
}
function detail(index) {
    gotoNewHtml(null, 0, 2);
}
function edit(index,parentId) {
    gotoNewHtml(index, 1, 2, parentId);
}
function gotoNewHtml(index, type, advType, parentId) {
    $.ajaxSetup({cache: false});
    $(".mainDiv").load('htmlWeb/systemAddAdverHtml', '', function () {
        $("#id").val(index);
        $("#opType").val(type);
        $("#advType").val(advType);
        $("#parentId").val(parentId);
        $.getScript("../../by-webhtml/assets/js/system/system_addAdver.js");
    });
}
function init() {
    if($("#parentId").val() != '' && $("#parentId").val() > 0){
        $("#add").css("display",'block');
    }
    $("#add").click(function () {
        gotoNewHtml(null, 0, 2, $("#parentId").val());
    });
    $("#pager a,.pageBut").click(function () {
        if ($(this).attr("class") == "first") {
            curPage = 1;
        } else if ($(this).attr("class") == "previous") {
            curPage = (curPage - 1) <= 1 ? 1 : curPage - 1;
        } else if ($(this).attr("class") == "netx") {
            curPage = (curPage + 1) > totalPage ? totalPage : (curPage + 1);
        } else if ($(this).attr("class") == "last") {
            curPage = totalPage;
        } else if ($(this).attr("class") == "pageBut") {
            if ($(".pageTxt").val() == "") return;
            var pageTex = (Number)($(".pageTxt").val());
            if (isNaN(pageTex)) return;
            curPage = pageTex >= totalPage ? totalPage : pageTex;
            $(".pageTxt").val("");
        }
        getGrid(getParam());
    });
}
function getPager() {
    if (curPage > totalPage) curPage = totalPage;
    if (curPage < 1) curPage = 1;
    $('.total').html(total);
    $('.curPage').html(curPage);
    $('.totalPage').html(totalPage);
}
$(function () {
    init();
    getGrid(getParam());
});